using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
   public class tbTourmoDAL
    {
        private SqlConnection cn = null;
        public tbTourmoDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTourmo tm)
        {
            SqlCommand cm = new SqlCommand("sp_tourmo_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matour",tm.Matour);
            cm.Parameters.AddWithValue("@ngaymo",tm.Ngaymo );
            cm.Parameters.AddWithValue("@ngaykhoihanh", tm.Ngaykhoihanh);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTourmo tm)
        {
            SqlCommand cm = new SqlCommand("sp_tourmo_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matourmo", tm.Matourmo);
            cm.Parameters.AddWithValue("@matour", tm.Matour);
            cm.Parameters.AddWithValue("@ngaymo", tm.Ngaymo);
            cm.Parameters.AddWithValue("@ngaykhoihanh", tm.Ngaykhoihanh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tourmo_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matourmo", iD);
            cm.ExecuteNonQuery();
        }
        public tbTourmoCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tourmo_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTourmoCollection tmColl = new tbTourmoCollection();

            while (dr.Read()) 
            {
                tbTourmo tm = new tbTourmo();
                tm.Matourmo = int.Parse(dr.GetValue(0).ToString());
                tm.Matour = int.Parse(dr.GetValue(1).ToString());
                tm.Ngaymo = DateTime.Parse(dr.GetValue(2).ToString());
                tm.Ngaykhoihanh = DateTime.Parse(dr.GetValue(3).ToString());
                tmColl.Add(tm);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tmColl;
        }
    }
}
